home *** CD-ROM | disk | FTP | other *** search
/ Aminet 49 / Aminet 49 (2002)(GTI - Schatztruhe)[!][Jun 2002].iso / Aminet / util / sys / AmberRAM.lha / AmberRAM / Source / handler.c < prev    next >
C/C++ Source or Header  |  2002-03-03  |  11KB  |  453 lines

  1. /*
  2.  
  3. File: handler.c
  4. Author: Neil Cafferkey
  5. Copyright (C) 2001-2002 Neil Cafferkey
  6.  
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation; either version 2
  10. of the License, or (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  20. MA 02111-1307, USA.
  21.  
  22. */
  23.  
  24. #include "handler_protos.h"
  25.  
  26. /* Global variables */
  27.  
  28. struct ExecBase *SysBase;
  29. struct DosLibrary *DOSBase;
  30. struct UtilityBase *UtilityBase;
  31. struct LocaleBase *LocaleBase;
  32.  
  33. /* Data prototypes */
  34.  
  35. extern struct ExecBase *AbsExecBase;
  36.  
  37. /* Global constants */
  38.  
  39. const TEXT version_string[]="$VER: amber-ram-handler 1.4 (3.3.2002)";
  40. const TEXT dos_name[]=DOSNAME;
  41.  
  42. /* Function prototypes */
  43.  
  44. static struct DosPacket *GetPacket(struct MsgPort *port);
  45. static VOID ReplyPacket(struct MsgPort *proc_port,struct DosPacket *packet,
  46.    PINT result1,PINT result2);
  47. static STRPTR BStr(UBYTE *b_str);
  48. static STRPTR BStr2(UBYTE *b_str);
  49.  
  50.  
  51.  
  52. LONG Main()
  53. {
  54.    struct Handler *handler;
  55.    struct MsgPort *proc_port;
  56.    struct DosPacket *packet;
  57.    struct DosLibrary *dos_base;
  58.    PINT result;
  59.    BOOL exit;
  60.    LONG link_type;
  61.    APTR link_target;
  62.    ULONG port_signals;
  63.    struct Message *message;
  64.  
  65.    /* Open libraries */
  66.  
  67.    SysBase=AbsExecBase;
  68.    dos_base=(struct DosLibrary *)OpenLibrary(dos_name,DOS_VERSION);
  69.    if(!dos_base)
  70.       return RETURN_FAIL;
  71.    DOSBase=dos_base;
  72.  
  73.    /* Process start-up packet */
  74.  
  75.    proc_port=&((*(struct Process *)FindTask(NULL)).pr_MsgPort);
  76.    handler=NULL;
  77.  
  78.    WaitPort(proc_port);
  79.    packet=GetPacket(proc_port);
  80.  
  81.    handler=CmdStartup(BStr(BADDR(packet->dp_Arg1)),
  82.       BADDR(packet->dp_Arg3),proc_port);
  83.    exit=handler==NULL;
  84.  
  85.    ReplyPacket(proc_port,packet,DOSBOOL(!exit),IoErr());
  86.  
  87.    /* Set up port signals */
  88.  
  89.    port_signals=1<<(proc_port->mp_SigBit);
  90.    Signal(proc_port->mp_SigTask,port_signals);
  91.    if(!exit)
  92.       port_signals|=1<<(handler->notify_port->mp_SigBit);
  93.  
  94.    /* Service packet requests */
  95.  
  96.    while(!exit)
  97.    {
  98.       Wait(port_signals);
  99.  
  100.       /* Accept returning messages from notification clients */
  101.  
  102.       while((message=GetMsg(handler->notify_port))!=NULL)
  103.       {
  104.          ReceiveNotifyReply(handler,(APTR)message);
  105.       }
  106.  
  107.       /* Service packet requests */
  108.  
  109.       while((packet=GetPacket(proc_port))!=NULL)
  110.       {
  111.          result=DOSTRUE;
  112.  
  113.          switch(packet->dp_Type)
  114.          {
  115.          case ACTION_DIE:
  116.  
  117.             if(!CmdDie(handler))
  118.                result=DOSFALSE;
  119.             exit=result;
  120.             break;
  121.  
  122.          case ACTION_IS_FILESYSTEM:
  123.  
  124.             if(!CmdIsFileSystem())
  125.                result=DOSFALSE;
  126.             break;
  127.  
  128.          case ACTION_FINDOUTPUT:
  129.  
  130.             if(!CmdFind(handler,BADDR(packet->dp_Arg1),
  131.                BADDR(packet->dp_Arg2),BStr(BADDR(packet->dp_Arg3)),
  132.                MODE_NEWFILE))
  133.                result=DOSFALSE;
  134.             break;
  135.  
  136.          case ACTION_FINDUPDATE:
  137.  
  138.             if(!CmdFind(handler,BADDR(packet->dp_Arg1),
  139.                BADDR(packet->dp_Arg2),BStr(BADDR(packet->dp_Arg3)),
  140.                MODE_READWRITE))
  141.                result=DOSFALSE;
  142.             break;
  143.  
  144.          case ACTION_FINDINPUT:
  145.  
  146.             if(!CmdFind(handler,BADDR(packet->dp_Arg1),
  147.                BADDR(packet->dp_Arg2),BStr(BADDR(packet->dp_Arg3)),
  148.                MODE_OLDFILE))
  149.                result=DOSFALSE;
  150.             break;
  151.  
  152.          case ACTION_FH_FROM_LOCK:
  153.  
  154.             result=CmdFHFromLock(BADDR(packet->dp_Arg1),
  155.                BADDR(packet->dp_Arg2));
  156.             break;
  157.  
  158.          case ACTION_END:
  159.  
  160.             if(!CmdEnd(handler,(APTR)packet->dp_Arg1))
  161.                result=DOSFALSE;
  162.             break;
  163.  
  164.          case ACTION_READ:
  165.  
  166.             result=CmdRead((struct Opening *)packet->dp_Arg1,
  167.                (UBYTE *)packet->dp_Arg2,packet->dp_Arg3);
  168.             break;
  169.  
  170.          case ACTION_WRITE:
  171.  
  172.             result=CmdWrite(handler,(struct Opening *)packet->dp_Arg1,
  173.                (UBYTE *)packet->dp_Arg2,packet->dp_Arg3);
  174.             break;
  175.  
  176.          case ACTION_SEEK:
  177.             result=CmdSeek((struct Opening *)packet->dp_Arg1,
  178.                packet->dp_Arg2,packet->dp_Arg3);
  179.             break;
  180.  
  181.          case ACTION_SET_FILE_SIZE:
  182.  
  183.             result=CmdSetFileSize(handler,(APTR)packet->dp_Arg1,
  184.                packet->dp_Arg2,packet->dp_Arg3);
  185.             break;
  186.  
  187.          case ACTION_LOCATE_OBJECT:
  188.  
  189.             result=MKBADDR(CmdLocateObject(handler,BADDR(packet->dp_Arg1),
  190.                BStr(BADDR(packet->dp_Arg2)),packet->dp_Arg3));
  191.             break;
  192.  
  193.          case ACTION_FREE_LOCK:
  194.  
  195.             if(!CmdFreeLock(handler,BADDR(packet->dp_Arg1)))
  196.                result=DOSFALSE;
  197.             break;
  198.  
  199.          case ACTION_COPY_DIR:
  200.  
  201.             result=MKBADDR(CmdCopyDir(handler,BADDR(packet->dp_Arg1)));
  202.             break;
  203.  
  204.          case ACTION_COPY_DIR_FH:
  205.  
  206.             result=MKBADDR(CmdCopyDirFH(handler,(APTR)packet->dp_Arg1));
  207.             break;
  208.  
  209.          case ACTION_PARENT:
  210.  
  211.             result=MKBADDR(CmdParent(handler,BADDR(packet->dp_Arg1)));
  212.             break;
  213.  
  214.          case ACTION_PARENT_FH:
  215.  
  216.             result=MKBADDR(CmdParentFH(handler,(APTR)packet->dp_Arg1));
  217.             break;
  218.  
  219.          case ACTION_SAME_LOCK:
  220.  
  221.             if(!CmdSameLock(BADDR(packet->dp_Arg1),BADDR(packet->dp_Arg2)))
  222.                result=DOSFALSE;
  223.             break;
  224.  
  225.          case ACTION_CREATE_DIR:
  226.  
  227.             result=MKBADDR(CmdCreateDir(handler,BADDR(packet->dp_Arg1),
  228.                BStr(BADDR(packet->dp_Arg2))));
  229.             break;
  230.  
  231.          case ACTION_EXAMINE_OBJECT:
  232.  
  233.             if(!CmdExamineObject(handler,BADDR(packet->dp_Arg1),
  234.                BADDR(packet->dp_Arg2)))
  235.                result=DOSFALSE;
  236.             break;
  237.  
  238.          case ACTION_EXAMINE_FH:
  239.  
  240.             if(!CmdExamineFH(handler,(APTR)packet->dp_Arg1,
  241.                BADDR(packet->dp_Arg2)))
  242.                result=DOSFALSE;
  243.             break;
  244.  
  245.          case ACTION_EXAMINE_NEXT:
  246.  
  247.             if(!CmdExamineNext(handler,BADDR(packet->dp_Arg2)))
  248.                result=DOSFALSE;
  249.             break;
  250.  
  251.          case ACTION_INFO:
  252.  
  253.             if(!CmdInfo(handler,BADDR(packet->dp_Arg2)))
  254.                result=DOSFALSE;
  255.             break;
  256.  
  257.          case ACTION_DISK_INFO:
  258.  
  259.             if(!CmdInfo(handler,BADDR(packet->dp_Arg1)))
  260.                result=DOSFALSE;
  261.             break;
  262.  
  263.          case ACTION_SET_PROTECT:
  264.  
  265.             if(!CmdSetProtect(handler,BADDR(packet->dp_Arg2),
  266.                BStr(BADDR(packet->dp_Arg3)),packet->dp_Arg4))
  267.                result=DOSFALSE;
  268.             break;
  269.  
  270.          case ACTION_SET_COMMENT:
  271.  
  272.             if(!CmdSetComment(handler,BADDR(packet->dp_Arg2),
  273.                BStr(BADDR(packet->dp_Arg3)),BStr2(BADDR(packet->dp_Arg4))
  274.                ))
  275.                result=DOSFALSE;
  276.             break;
  277.  
  278.          case ACTION_RENAME_OBJECT:
  279.  
  280.             if(!CmdRenameObject(handler,BADDR(packet->dp_Arg1),
  281.                BStr(BADDR(packet->dp_Arg2)),BADDR(packet->dp_Arg3),
  282.                BStr2(BADDR(packet->dp_Arg4))))
  283.                result=DOSFALSE;
  284.             break;
  285.  
  286.          case ACTION_RENAME_DISK:
  287.  
  288.             if(!CmdRenameDisk(handler,BStr(BADDR(packet->dp_Arg1))))
  289.                result=DOSFALSE;
  290.             break;
  291.  
  292.          case ACTION_SET_DATE:
  293.  
  294.             if(!CmdSetDate(handler,BADDR(packet->dp_Arg2),
  295.                BStr(BADDR(packet->dp_Arg3)),(APTR)packet->dp_Arg4))
  296.                result=DOSFALSE;
  297.             break;
  298.  
  299.          case ACTION_DELETE_OBJECT:
  300.  
  301.             if(!CmdDeleteObject(handler,BADDR(packet->dp_Arg1),
  302.                BStr(BADDR(packet->dp_Arg2))))
  303.                result=DOSFALSE;
  304.             break;
  305.  
  306.          case ACTION_CURRENT_VOLUME:
  307.  
  308.             result=MKBADDR(CmdCurrentVolume(handler));
  309.             break;
  310.  
  311.          case ACTION_CHANGE_MODE:
  312.  
  313.             result=CmdChangeMode(packet->dp_Arg1,BADDR(packet->dp_Arg2),
  314.                packet->dp_Arg3);
  315.             break;
  316.  
  317.          case ACTION_MAKE_LINK:
  318.  
  319.             link_target=(APTR)packet->dp_Arg3;
  320.             link_type=packet->dp_Arg4;
  321.             if(link_type==LINK_HARD)
  322.                link_target=BADDR(link_target);
  323.             if(!CmdMakeLink(handler,BADDR(packet->dp_Arg1),
  324.                BStr(BADDR(packet->dp_Arg2)),link_target,link_type))
  325.                result=DOSFALSE;
  326.             break;
  327.  
  328.          case ACTION_WRITE_PROTECT:
  329.  
  330.             if(!CmdWriteProtect(handler,packet->dp_Arg1,
  331.                (ULONG)packet->dp_Arg2))
  332.                result=DOSFALSE;
  333.             break;
  334.  
  335.          case ACTION_FLUSH:
  336.  
  337.             if(!CmdFlush())
  338.                result=DOSFALSE;
  339.             break;
  340.  
  341.          case ACTION_ADD_NOTIFY:
  342.  
  343.             if(!CmdAddNotify(handler,(APTR)packet->dp_Arg1))
  344.                result=DOSFALSE;
  345.             break;
  346.  
  347.          case ACTION_REMOVE_NOTIFY:
  348.  
  349.             if(!CmdRemoveNotify(handler,(APTR)packet->dp_Arg1))
  350.                result=DOSFALSE;
  351.             break;
  352.  
  353.          default:
  354.             result=DOSFALSE;
  355.             SetIoErr(ERROR_ACTION_NOT_KNOWN);
  356.          }
  357.  
  358.          ReplyPacket(proc_port,packet,result,IoErr());
  359.       }
  360.  
  361.    }
  362.  
  363.    CloseLibrary((APTR)DOSBase);
  364.  
  365.    return RETURN_OK;
  366. }
  367.  
  368.  
  369.  
  370. static struct DosPacket *GetPacket(struct MsgPort *port)
  371. {
  372.    struct Message *message;
  373.    struct DosPacket *packet;
  374.  
  375.    message=GetMsg(port);
  376.    if(message!=NULL)
  377.       packet=(struct DosPacket *)((struct Node *)message)->ln_Name;
  378.    else
  379.       packet=NULL;
  380.  
  381.    return packet;
  382. }
  383.  
  384.  
  385.  
  386. static VOID ReplyPacket(struct MsgPort *proc_port,struct DosPacket *packet,
  387.    PINT result1,PINT result2)
  388. {
  389.    struct MsgPort *port;
  390.    struct Message *message;
  391.  
  392.    port=packet->dp_Port;
  393.    message=packet->dp_Link;
  394.  
  395.    packet->dp_Port=proc_port;
  396.    packet->dp_Res1=result1;
  397.    packet->dp_Res2=result2;
  398.  
  399.    PutMsg(port,message);
  400.  
  401.    return;
  402. }
  403.  
  404.  
  405.  
  406. TEXT b_buffer[256];
  407. TEXT b_buffer2[256];
  408.  
  409.  
  410.  
  411. static STRPTR BStr(UBYTE *b_str)
  412. {
  413.    UBYTE length;
  414.  
  415.    length=*b_str;
  416.    if(length!=0)
  417.       CopyMem(b_str+1,b_buffer,length);
  418.    *(b_buffer+length)='\0';
  419.  
  420.    return b_buffer;
  421. }
  422.  
  423.  
  424.  
  425. static STRPTR BStr2(UBYTE *b_str)
  426. {
  427.    UBYTE length;
  428.  
  429.    length=*b_str;
  430.    if(length!=0)
  431.       CopyMem(b_str+1,b_buffer2,length);
  432.    *(b_buffer2+length)='\0';
  433.  
  434.    return b_buffer2;
  435. }
  436.  
  437.  
  438.  
  439. UBYTE *MkBStr(STRPTR str)
  440. {
  441.    UBYTE length;
  442.  
  443.    length=StrLen(str);
  444.    if(length!=0)
  445.       CopyMem(str,b_buffer+1,length);
  446.    *b_buffer=length;
  447.  
  448.    return b_buffer;
  449. }
  450.  
  451.  
  452.  
  453.